﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Enum;

namespace RooHui.OA.Biz
{
    public class ProcessDefBiz
    {
        public static List<ProcessDef> GetProcessDefList()
        {
            try
            {

                var processDefList = new ProcessDef().FindAll();
                    return processDefList;
                
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        /// <summary>
        /// 获取所有流程信息
        /// </summary>
        /// <returns></returns>
        public static List<ProcessDef> GetProcessDefListByPage(int page, int pageSize, string key, ref int count)
        {
            try
            {
                using (Context ctx = new Context())
                {
                     
                    var expression = from c in ctx.ProcessDef
                                     where c.ProcessDefName.Contains(key) && c.Status != (int)ItemStatus.Delete
                                     orderby c.ProcessDefName
                                     select c;
                    
                    count = expression.Count();
                    var data = expression
                        .Skip((page - 1) * pageSize)
                        .Take(pageSize)
                        .ToList().Select(p => new ProcessDef().Set(p)).ToList();
                    return data;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static ProcessDef AddProcessDef(string processDefName, string xmlUrl, int processType,string createUser)
        {
            try
            {
                var processDef = new ProcessDef()
                {
                    ProcessDefName = processDefName,
                    XmlUrl=xmlUrl,
                    ProcessType = (ProcessType)processType,
                    Status=ItemStatus.Enable,
                    CreateTime=DateTime.Now,
                    CreateUser=createUser
                };

                processDef = processDef.PostAdd();
                return processDef;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static ProcessDef EditProcessDef(int processDefID,string processDefName, string xmlUrl, int processType)
        {
            try
            {
                var processDef = new ProcessDef().Find(processDefID.ToString());
                processDef.ProcessDefName = processDefName;
                processDef.XmlUrl = xmlUrl;
                processDef.ProcessType = (ProcessType)processType;
                processDef = processDef.PostModify();
                return processDef;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static ProcessDef DeleteProcessDef(int processDefID)
        {
            try
            {
                var processDef = new ProcessDef().Find(processDefID.ToString());
                processDef.Status = ItemStatus.Delete; 
                processDef = processDef.PostModify();
                //UserRoleBiz.UserAuthorizeDelete(userName);
                //还需要删除节点和条件表数据
                return processDef;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static ProcessDef ChangeStatus(int processDefID, int status)
        {
            try
            {
                var processDef = new ProcessDef().Find(processDefID.ToString());
                processDef.Status = (ItemStatus)status;
                processDef = processDef.PostModify();
                return processDef;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static bool ProcessDefExsit(string processDefName,int defID=0)
        {
            bool isTrue = false;
            try
            {
                var user = new ProcessDef().Find(c => c.ProcessDefName == processDefName && c.Status != (int)ItemStatus.Delete && c.ID != defID);
                if (user != null)
                {
                    isTrue = true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return isTrue;
        }
    }
}
